package test

import (
	"net/http"
	kinit "longblog/work/base/initialize"
	kroute "longblog/work/base/route"
	kcode "longblog/work/code"
	kbase "longblog/work/control/base"
	kdaotest "longblog/work/dao/test"

	"github.com/gin-gonic/gin"
)

type Test struct {
}

func NewTest() *Test {
	return &Test{}
}
func (ts *Test) Load() []kroute.RouteWrapStruct {
	m := make([]kroute.RouteWrapStruct, 0)

	//test
	m = append(m, kbase.Wrap("GETPOST", "/test/test", ts.test, kbase.MIDDLE_TYPE_CHECK_PERMISSION))

	return m
}

//-----------------------------------------------------------------------------------

type testBind struct {
	Status int64 `form:"status"  binding:""`
}

func (ts *Test) test(c *gin.Context) {
	var param testBind
	if err := c.ShouldBind(&param); err != nil {
		kinit.LogError.Println(err)
		kbase.SendErrorTokenJsonStr(c, kcode.PARAM_WRONG, "")
		return
	}

	g := make(gin.H)
	_, ok := kbase.CheckGetPostParam(c, "status")
	if !ok {
		g["lists"] = kdaotest.ObjTest.GetAll(nil)
	} else {
		g["lists"] = kdaotest.ObjTest.GetByStatus(nil, param.Status)
	}

	c.HTML(http.StatusOK, "test/test.html", g)
}

//-----------------------------------------------------------------------------------
